package com.xkdx.guangguang.fragment.my.setup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.xkdx.caipiao.R;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.fragment.my.setup.ExchangeShopModule.ProdecuListDetail;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.util.AsyncImageLoader;

public class MoreExchangeFragment extends BaseFragment {
	View view = null;

	ImageLoader imageLoader = ImageLoader.getInstance();
	DisplayImageOptions options;
	MyAdapter adapter;
	String tag;
	int type = 0;

	// private Button brand_search;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		if (imageLoader.isInited()) {
			imageLoader.destroy();
		}
		imageLoader.init(ImageLoaderConfiguration.createDefault(getActivity()));
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.empty_photo)
				.showImageForEmptyUri(R.drawable.empty_photo)
				.showImageOnFail(R.drawable.empty_photo)
				.resetViewBeforeLoading().cacheOnDisc()
				.imageScaleType(ImageScaleType.EXACTLY)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
		tag = getArguments().getString("tag");
		if (tag.equals("购物类商品") && ExchangeShopFragment.shopProductList != null
				&& ExchangeShopFragment.shopProductList.size() != 0) {
			adapter = new MyAdapter(getActivity(),
					ExchangeShopFragment.shopProductList);
			type = 1;
		} else if (tag.equals("美食类商品")
				&& ExchangeShopFragment.foodProductList != null
				&& ExchangeShopFragment.foodProductList.size() != 0) {
			type = 3;
			adapter = new MyAdapter(getActivity(),
					ExchangeShopFragment.foodProductList);
		} else if (tag.equals("娱乐类商品")
				&& ExchangeShopFragment.entertaimentProductList != null
				&& ExchangeShopFragment.entertaimentProductList.size() != 0) {
			type = 2;
			adapter = new MyAdapter(getActivity(),
					ExchangeShopFragment.entertaimentProductList);
		} else if (tag.equals("生活类商品")
				&& ExchangeShopFragment.lifeProductList != null
				&& ExchangeShopFragment.lifeProductList.size() != 0) {
			type = 4;
			adapter = new MyAdapter(getActivity(),
					ExchangeShopFragment.lifeProductList);
		}

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.more_product, container, false);

		init();

		return view;
	}

	private void init() {

		Button back = (Button) view.findViewById(R.id.more_brand_back);

		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				FragmentManager fm = getFragmentManager();
				fm.popBackStack();
			}
		});
		TextView title = (TextView) view.findViewById(R.id.more_brand_title);
		title.setText(getArguments().getString("tag"));
		GridView gv = (GridView) view.findViewById(R.id.more_brand_gv);
		gv.setAdapter(adapter);
		gv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub

				switch (type) {
				case 1:
					FragmentTransaction ft = getFragmentManager()
							.beginTransaction();
					MyIntegralexChangeFrament brandShopFragment = new MyIntegralexChangeFrament();
					Bundle b = new Bundle();
					b.putString("Type", ExchangeShopFragment.shopProductList
							.get(position).getType());
					b.putString("ProductID",
							ExchangeShopFragment.shopProductList.get(position)
									.getProductID());
					brandShopFragment.setArguments(b);
					ft.replace(R.id.fragment, brandShopFragment);
					ft.addToBackStack(null);
					ft.commit();
					break;
				case 2:
					FragmentTransaction ft2 = getFragmentManager()
							.beginTransaction();
					MyIntegralexChangeFrament brandShopFragment2 = new MyIntegralexChangeFrament();
					Bundle b2 = new Bundle();
					b2.putString(
							"Type",
							ExchangeShopFragment.entertaimentProductList.get(
									position).getType());
					b2.putString(
							"ProductID",
							ExchangeShopFragment.entertaimentProductList.get(
									position).getProductID());
					brandShopFragment2.setArguments(b2);
					ft2.replace(R.id.fragment, brandShopFragment2);
					ft2.addToBackStack(null);
					ft2.commit();
					break;
				case 3:
					FragmentTransaction ft3 = getFragmentManager()
							.beginTransaction();
					MyIntegralexChangeFrament brandShopFragment3 = new MyIntegralexChangeFrament();
					Bundle b3 = new Bundle();
					b3.putString("Type", ExchangeShopFragment.foodProductList
							.get(position).getType());
					b3.putString("ProductID",
							ExchangeShopFragment.foodProductList.get(position)
									.getProductID());
					brandShopFragment3.setArguments(b3);
					ft3.replace(R.id.fragment, brandShopFragment3);
					ft3.addToBackStack(null);
					ft3.commit();
					break;
				case 4:
					FragmentTransaction ft4 = getFragmentManager()
							.beginTransaction();
					MyIntegralexChangeFrament brandShopFragment4 = new MyIntegralexChangeFrament();
					Bundle b4 = new Bundle();
					b4.putString("Type", ExchangeShopFragment.lifeProductList
							.get(position).getType());
					b4.putString("ProductID",
							ExchangeShopFragment.lifeProductList.get(position)
									.getProductID());
					brandShopFragment4.setArguments(b4);
					ft4.replace(R.id.fragment, brandShopFragment4);
					ft4.addToBackStack(null);
					ft4.commit();
					break;
				default:
					break;
				}
			}
		});

	}

	class MyAdapter extends BaseAdapter {

		String url;
		Context context;

		AsyncImageLoader asyncImageLoader;
		List<ProdecuListDetail> list = new ArrayList<ProdecuListDetail>();

		public MyAdapter(Context context,
				List<ProdecuListDetail> shopProductList) {
			super();

			this.list = shopProductList;
			this.context = context;

		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHodler viewHolder;
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(
						R.layout.brand_item, null);
				viewHolder = new ViewHodler();
				viewHolder.image = (ImageView) convertView
						.findViewById(R.id.brand_image);
				viewHolder.name = (TextView) convertView
						.findViewById(R.id.brand_item_name);
				viewHolder.ll = (LinearLayout) convertView
						.findViewById(R.id.brand_item_linearLayout);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHodler) convertView.getTag();
			}

			url = list.get(position).getLogo();
			viewHolder.name.setText(list.get(position).getProductName());

			imageLoader.displayImage(url, viewHolder.image, options);

			return convertView;
		}

		class ViewHodler {
			TextView name;
			ImageView image;
			LinearLayout ll;
		}

	}

	@Override
	public void findView() {
		// TODO Auto-generated method stub

	}

	@Override
	public void setOnClick() {
		// TODO Auto-generated method stub

	}

	@Override
	public void showLoading() {
		// TODO Auto-generated method stub

	}

	@Override
	public void showLoaded(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub

	}

	@Override
	public void showClick(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub

	}

	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub

	}

}
